<?php
namespace App\Services\Front;

use App\Services\BaseService as Service;
use Illuminate\Support\Facades\DB;

class BaseService extends Service
{
    /**
     * 服务类，写复杂的逻辑
     */

    //生产空码
    public function makeEmpty()
    {
        $num = 50;
        $total = $this->dbConn($this->dbQr)
            ->where('tmp_id', 0)
            ->count();
        if ($total >= $num) return true;
        for ($i = 0; $i <= $num; $i ++) {
            $showCode = time().rand(10000, 99999);
            $qrDatas = [
                'code' => $this->getCode(),
                'show_code' => $showCode,
                'addtime' => toDate(),
            ];
            $this->dbConn($this->dbQr)->insertGetId($qrDatas);
        }
        return true;
    }

    //生成新码
    public function getCode()
    {
        $code = toRandStr(rand(4, 6), 1);
        $qrOne = (array) $this->dbConn($this->dbQr)
            ->where('code', $code)
            ->first();
        if (!empty($qrOne)) {
            return $this->getCode();
        }
        return $code;
    }

    //返回格式
    public function toback($code = 0, $msg = '', $data = [], $total = 0)
    {
        $msg = $msg ? $msg : 'success';
        return array('code' => $code, 'msg' => $msg, 'data' => $data, 'total' => $total);
    }
}